use rusqlite::Connection;
use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime};
use chrono::format::ParseError;


pub(crate) fn create_tables() -> rusqlite::Result<()> {
    println!("begin init db ...");


    let conn = Connection::open("modbus_addrs.db")?;
    let mut stmt = conn.prepare("create table if not exists devices (
             id integer primary key,
             ip text not null,
             port integer not null,
             slave_id integer not null,
             create_user integer,
             create_time DATETIME,
             last_update_time DATETIME,
             last_update_user integer
         )")?;
    stmt.execute([])?;

    let mut stmt = conn.prepare("create table if not exists frames (
             id integer primary key,
             device_id integer not null references devices(id),
             fun_code varchar,
             start_addr varchar,
             endian_type integer,
             length int,
             frequency int,
             create_user integer,
             create_time DATETIME,
             last_update_time DATETIME,
             last_update_user integer
         )")?;
    stmt.execute([])?;
    println!("init db done ...");
    Ok(())
}